/*
 *   Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *   Licensed under the GNU General Public License v3 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *        http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *   under the License.
 */

package metaphor.service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * A simple application manager that allows objects to be associated with a name
 * which could be initialized by more advanced IOC controllers like Spring,
 * Tapestry or Mentawai. The idea here is not to replace their IOC managers but
 * to allow the application to plugged into them and providing a standard
 * API across all of them.<br />
 *
 * @author Mark P Ashworth <javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
public class DefaultApplicationManager extends AbstractApplicationManager {

    private Map<String, Object> beans = Collections.synchronizedMap(new HashMap<String, Object>());

    public void setBean(String name, Object value) {
        beans.put(name, value);
    }

    public Object getDao() throws ServiceException {
        return getBean(getDaoName(), getDaoClass());
    }

    /**
     * Returns the security manager
     * @return Security Manager
     * @throws ServiceException If the security manager could not be loaded
     */
    public metaphor.service.SecurityManager getSecurityManager() throws ServiceException {
        return (metaphor.service.SecurityManager) getBean(getSecurityManagerName(), getSecurityManagerClass());
    }

    public Map<String, Object> getBeans() {
        return beans;
    }

    public void setBeans(Map<String, Object> beans) {
        this.beans.clear();

        if (beans != null) {
            this.beans.putAll(beans);
        }
    }

    /**
     * Returns the bean with name
     * @param name The name of the bean
     * @return Object
     * @throws BeanNotFoundException If the bean does not exist in the manager
     */
    public Object getBean(String name) throws ServiceException {
        if (!beans.containsKey(name)) {
            throw new BeanNotFoundException("The bean with name `" + name + "` was not found.");
        }
        return beans.get(name);
    }

    /**
     * Returns the bean with name and specified type
     * @param name The name of the bean
     * @param beanClass The class type of the bean requested
     * @return Object
     * @throws BeanNotFoundException If the bean does not exist in the manager
     */
    public Object getBean(String name, Class beanClass) throws ServiceException {
        Object value = getBean(name);
        if (!beanClass.isInstance(value)) {
            throw new BeanNotFoundException("The bean with name `" + name + "` and type `" + beanClass + "`was not found.");
        }
        return value;
    }
}
